<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">修改密码</h3>
      <el-form-item prop="userName">
        <el-input v-model="registerForm.userName" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <!-- <el-form-item prop="email">
        <el-input v-model="registerForm.email" type="text" auto-complete="off" placeholder="邮箱">
          <svg-icon slot="prefix" icon-class="email" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item> -->
      <el-form-item prop="code">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="邮箱验证码"
          style="width: 63%"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <el-button
          class="email-code-button"
          :disabled="emailCodeButtonDisabled"
          @click="getEmailCode"
        >{{ emailCodeButtonText }}</el-button>
      </el-form-item>
      <el-form-item prop="newPassword">
        <el-input
          v-model="registerForm.newPassword"
          type="password"
          auto-complete="off"
          placeholder="新密码"
          @keyup.enter.native="handleUpdatenewPassword"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <!-- <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleUpdatenewPassword"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item> -->
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleUpdatenewPassword"
        >
          <span v-if="!loading">修改密码</span>
          <span v-else>提交中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="getLoginLink()">返回登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2025 心语云学生心理智慧服务平台 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register, sendCheckEmail, updatePassword } from "@/api/login";

export default {
  name: "Register",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      registerForm: {
        userName: "",
        newPassword: "",
        code: "",
      },
      registerRules: {
        userName: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, trigger: "blur", message: "请输入您的新密码" },
          { min: 5, max: 20, message: "密码长度必须介于 5 和 20 之间", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      captchaEnabled: true,
      emailCodeButtonDisabled: false,
      emailCodeButtonText: "获取邮箱验证码",
      countDown: 60
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleUpdatenewPassword() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          console.log(this.registerForm);
          updatePassword(this.registerForm).then(res => {
            this.$alert("<font color='green'>密码修改成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              // 保持isAdmin参数
              const loginPath = this.getLoginLink();
              this.$router.push(loginPath);
            }).catch(() => {});
          }).catch(error => {
            this.loading = false;
            this.$message.error(error.message || "密码修改失败，请重试");
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    getEmailCode() {
      // if (!this.registerForm.email) {
      //   this.$message.error("请先输入邮箱地址");
      //   return;
      // }
      
      if (!this.registerForm.userName) {
        this.$message.error("请先输入账号");
        return;
      }
      
      // 验证邮箱格式
      // const emailRegex = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      // if (!emailRegex.test(this.registerForm.email)) {
      //   this.$message.error("请输入正确的邮箱格式");
      //   return;
      // }
      
      this.emailCodeButtonDisabled = true;
      this.countDown = 60;
      
      // 发送请求获取邮箱验证码
      sendCheckEmail({
        email: this.registerForm.email,
        userName: this.registerForm.userName
      }).then(res => {
        this.$message.success("验证码已发送至您的邮箱");
        
        const timer = setInterval(() => {
          if (this.countDown > 0) {
            this.countDown--;
            this.emailCodeButtonText = `重新获取(${this.countDown}s)`;
          } else {
            clearInterval(timer);
            this.emailCodeButtonDisabled = false;
            this.emailCodeButtonText = "获取邮箱验证码";
          }
        }, 1000);
      }).catch(error => {
        this.emailCodeButtonDisabled = false;
        this.emailCodeButtonText = "获取邮箱验证码";
        this.$message.error(error.message || "邮箱验证码发送失败");
      });
    },
    
    // 获取登录链接，保持isAdmin参数
    getLoginLink() {
      const isAdmin = this.$route.query.isAdmin;
      if (isAdmin !== undefined) {
        return `/login?isAdmin=${isAdmin}`;
      }
      return '/login';
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
// 全局主体样式
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(-45deg, #667eea 0%, #764ba2 50%, #8b5cf6 100%);
  background-size: 400% 400%;
  animation: gradientShift 12s ease infinite;
  position: relative;
  overflow: hidden;
  font-family: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

  // 动态粒子背景
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle at 20% 50%, rgba(102, 126, 234, 0.3) 0%, transparent 50%),
                radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.3) 0%, transparent 50%),
                radial-gradient(circle at 40% 80%, rgba(120, 119, 198, 0.2) 0%, transparent 50%);
    animation: particleFloat 20s ease-in-out infinite alternate;
    z-index: 1;
  }

  // 几何装饰元素
  &::after {
    content: '';
    position: absolute;
    width: 600px;
    height: 600px;
    background: linear-gradient(45deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02));
    border-radius: 50%;
    top: -300px;
    right: -300px;
    animation: geometryRotate 30s linear infinite;
    z-index: 1;
  }
}

// 动画定义
@keyframes gradientShift {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

@keyframes particleFloat {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 1;
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
    opacity: 0.8;
  }
}

@keyframes geometryRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

// 标题样式 - 发光效果
.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #fff;
  font-size: 28px;
  font-weight: 800;
  text-shadow: 0 0 20px rgba(255, 255, 255, 0.5),
               0 0 40px rgba(120, 119, 198, 0.4),
               0 0 60px rgba(120, 119, 198, 0.3);
  letter-spacing: 2px;
  position: relative;
  animation: titleGlow 3s ease-in-out infinite alternate;
  background: linear-gradient(45deg, #fff, #e0e7ff, #c7d2fe);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.3));

  &::after {
    content: '';
    position: absolute;
    bottom: -12px;
    left: 50%;
    transform: translateX(-50%);
    width: 80px;
    height: 3px;
    background: linear-gradient(90deg, #667eea, #764ba2, #8b5cf6);
    border-radius: 3px;
    box-shadow: 0 0 10px rgba(102, 126, 234, 0.5);
    animation: lineGlow 2s ease-in-out infinite alternate;
  }
}

@keyframes titleGlow {
  0% {
    text-shadow: 0 0 20px rgba(255, 255, 255, 0.5),
                 0 0 40px rgba(120, 119, 198, 0.4),
                 0 0 60px rgba(120, 119, 198, 0.3);
  }
  100% {
    text-shadow: 0 0 30px rgba(255, 255, 255, 0.8),
                 0 0 60px rgba(120, 119, 198, 0.6),
                 0 0 90px rgba(120, 119, 198, 0.5);
  }
}

@keyframes lineGlow {
  0% {
    box-shadow: 0 0 10px rgba(102, 126, 234, 0.5);
    transform: translateX(-50%) scaleX(1);
  }
  100% {
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.8);
    transform: translateX(-50%) scaleX(1.1);
  }
}

// 主表单样式 - 磨砂玻璃效果
.register-form {
  position: relative;
  z-index: 10;
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  width: 480px;
  padding: 40px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15),
              0 0 0 1px rgba(255, 255, 255, 0.1) inset,
              0 8px 32px rgba(120, 119, 198, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transform: translateY(0);
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
  animation: formSlideIn 0.8s ease-out forwards;
  
  // 新增的内发光效果
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 24px;
    padding: 1px;
    background: linear-gradient(45deg, 
      rgba(255, 255, 255, 0.1), 
      rgba(102, 126, 234, 0.1), 
      rgba(139, 92, 246, 0.1),
      rgba(255, 255, 255, 0.1)
    );
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask-composite: exclude;
    -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    animation: borderRotate 6s linear infinite;
    z-index: -1;
  }

  &:hover {
    transform: translateY(-8px);
    box-shadow: 0 35px 70px rgba(0, 0, 0, 0.2),
                0 0 0 1px rgba(255, 255, 255, 0.15) inset,
                0 12px 48px rgba(120, 119, 198, 0.3);
  }
  
  .el-input {
    height: 52px;
    margin-bottom: 16px;
    
    input {
      height: 52px;
      padding-left: 50px;
      background: rgba(255, 255, 255, 0.05);
      border-radius: 16px;
      border: 1px solid rgba(255, 255, 255, 0.1);
      color: #fff;
      font-size: 15px;
      font-weight: 400;
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);
      
      &::placeholder {
        color: rgba(255, 255, 255, 0.5);
        font-weight: 400;
      }

      &:focus {
        background: rgba(255, 255, 255, 0.1);
        border-color: rgba(102, 126, 234, 0.5);
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1),
                    0 8px 25px rgba(102, 126, 234, 0.15);
        transform: translateY(-2px);
      }
    }
  }
  
  .input-icon {
    height: 52px;
    width: 24px;
    margin-left: 16px;
    color: rgba(255, 255, 255, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.3s ease;
  }
  
  .el-form-item {
    margin-bottom: 20px;
    
    &:focus-within .input-icon {
      color: rgba(102, 126, 234, 0.8);
      transform: scale(1.1);
    }
  }
  
  .el-button {
    height: 52px;
    border-radius: 16px;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 1px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: #fff;
    position: relative;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
      transition: left 0.5s;
    }

    &:hover {
      transform: translateY(-3px);
      box-shadow: 0 15px 35px rgba(102, 126, 234, 0.4);
      
      &::before {
        left: 100%;
      }
    }

    &:active {
      transform: translateY(-1px);
      box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
    }
  }
  
  .link-type {
    color: rgba(255, 255, 255, 0.8);
    text-decoration: none;
    font-weight: 500;
    margin-top: 15px;
    display: inline-block;
    transition: all 0.3s ease;
    position: relative;
    
    &::after {
      content: '';
      position: absolute;
      bottom: -2px;
      left: 0;
      width: 0;
      height: 1px;
      background: linear-gradient(90deg, #667eea, #764ba2);
      transition: width 0.3s ease;
    }

    &:hover {
      color: #fff;
      
      &::after {
        width: 100%;
      }
    }
  }
}

@keyframes formSlideIn {
  0% {
    opacity: 0;
    transform: translateY(40px) scale(0.95);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes borderRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

// 其他组件样式
.register-tip {
  font-size: 13px;
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  margin-top: 15px;
}

.register-code {
  width: 33%;
  height: 52px;
  float: right;
  
  img {
    height: 100%;
    cursor: pointer;
    vertical-align: middle;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    
    &:hover {
      transform: scale(1.05);
      border-color: rgba(102, 126, 234, 0.3);
      box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
    }
  }
}

// 底部版权信息
.el-register-footer {
  height: 50px;
  line-height: 50px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-family: 'Inter', 'Segoe UI', sans-serif;
  font-size: 13px;
  letter-spacing: 1px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  z-index: 10;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.1) 100%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.register-code-img {
  height: 52px;
}

// 邮箱验证码按钮样式
.email-code-button {
  width: 36%;
  height: 52px;
  border-radius: 16px;
  margin-left: 1%;
  font-size: 13px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  letter-spacing: 0.5px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
  }
  
  &:hover:not([disabled]) {
    transform: translateY(-2px);
    box-shadow: 0 12px 25px rgba(102, 126, 234, 0.4);
    
    &::before {
      left: 100%;
    }
  }
  
  &:active:not([disabled]) {
    transform: translateY(0px);
    box-shadow: 0 6px 15px rgba(102, 126, 234, 0.3);
  }
  
  &[disabled] {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.5);
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
    
    &::before {
      display: none;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .register-form {
    width: 90%;
    max-width: 400px;
    padding: 30px 25px;
    margin: 20px;
  }

  .title {
    font-size: 24px;
    margin-bottom: 25px;
  }
  
  .email-code-button {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .register {
    padding: 20px;
  }
  
  .register-form {
    width: 95%;
    padding: 25px 20px;
  }

  .title {
    font-size: 22px;
  }
  
  .email-code-button {
    font-size: 10px;
  }
  
  .el-register-footer {
    font-size: 11px;
    padding: 0 10px;
  }
}

// 高分辨率屏幕优化
@media (min-width: 1440px) {
  .register-form {
    width: 520px;
    padding: 50px;
  }
  
  .title {
    font-size: 32px;
  }
}

// 深色模式兼容
@media (prefers-color-scheme: dark) {
  .register {
    background: linear-gradient(-45deg, #1a1a2e 0%, #16213e 50%, #2d1b69 100%);
  }
}

// 动画性能优化
@media (prefers-reduced-motion: reduce) {
  .register,
  .register::before,
  .register::after,
  .title,
  .register-form {
    animation: none !important;
    transition: none !important;
  }
}
</style>
